// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	dataset_sitc.dta

// outputs:	agg-tar-trade.csv
//			ecm-lp.csv
//			gap-elast.csv
//			gap-applied.csv

// The output of this file is used to create figures 1-4 (f1--f4). 
// The `s53`' section produces the regression output discussed in section 5.3 of the manuscript.
// The `cemp` section computes estimated needed in the employment analysis.    

set varabbrev off
clear all 


timer on 1
global dir_int "../20 Intermediate files/"

// Set to 1 to produce the output for the associated figure. Figure 2 takes some time.
loc f1 1
loc f2 1
loc f3 1
loc f4 1
loc s53 1
loc cemp 1

////////////////////////////////////////////////////////////////////////////////
// Figure 1
if `f1' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	bysort cty year: egen temp_val_cty = sum(v_jst)
	bysort year: egen temp_val_tot = sum(v_jst)
	gen tempshare=temp_val_cty/temp_val_tot
	replace tempshare=log(tempshare)
	bysort year cty: egen temp_tar_unwgt = mean(tariff_jst)
	bysort year cty: egen temp_tar_unwgt_p25 = pctile(tariff_jst), p(25)
	bysort year cty: egen temp_tar_unwgt_p50 = pctile(tariff_jst), p(50)
	bysort year cty: egen temp_tar_unwgt_p75 = pctile(tariff_jst), p(75)

	egen temptag=tag(cty year)
	loc country "china"
	gen temp = temptag==1 & cty=="`country'"
	loc trade tempshare
	loc tariff temp_tar_unwgt

	export delimited `tariff'_p50  `tariff'_p25 year `tariff'_p75 `trade' if temp==1 using "${dir_int}agg-tar-trade.csv", replace
}

////////////////////////////////////////////////////////////////////////////////
// Figure 2
if `f2' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009
	
	loc treated chn
	gen temp = 1 & phase==0 & fta==0 & (`treated'==1 | ntr==1)
	xtset id year
	eststo clear
	eststo : qui reghdfe D.ltariff_jst if temp==1, absorb(sitcn#year ctyn#year sitcn#ctyn) vce(cluster sitcn#year) res(res_tar)
	export delimited res_tar year if chn==1 using "${dir_int}res_tar.csv", replace


	// Figure 2A: ECM and LP estimates
	loc tstat 1.96
	loc tmax 25
	loc fe="absorb(sitcn#year ctyn#year)"
	loc indicator="ibn.chn#c."
	loc indicator2="i1.chn#c."
	loc y lv_jst
	loc x ltariff_jst
	loc treated chn
	loc cluster="cluster sitcn#year"
	loc controls=""
	gen temp1 = phase==0 & fta==0 & (`treated'==1 | ntr==1)
	gen temp2 = temp1==1 & year==1979
	gen temp3 = temp1==1 & year>1979
	eststo clear
	xtset id year
	set more off
	
 	forv z = 2/2 {
 		gen temp_beta`z' = .
 		gen temp_ul`z' = .
 		gen temp_ll`z' = .
 		forv t=1/`tmax' {
 			if `t'==1 {
 				eststo : qui reghdfe F`t'.S`t'.`y' `indicator'F`t'.S`t'.`x' if temp`z'==1, `fe'
 			}
 			else {
 				eststo : qui ivreghdfe F`t'.S`t'.`y' (`indicator'F`t'.S`t'.`x'=`indicator'F1.S1.`x') if temp`z'==1, `fe'
 			}
 			replace temp_beta`z' = -_b[`indicator2'F`t'.S`t'.`x'] if _n==`t'
 			replace temp_ul`z' = -_b[`indicator2'F`t'.S`t'.`x'] + `tstat'*_se[`indicator2'F`t'.S`t'.`x'] if _n==`t'
 			replace temp_ll`z' = -_b[`indicator2'F`t'.S`t'.`x'] - `tstat'*_se[`indicator2'F`t'.S`t'.`x'] if _n==`t'
 			}
 	}

	loc treated chn
	loc cluster="cluster sitcn#year"
	loc controls="lshipping_jst"
	loc fe="sitcn#year ctyn#year sitcn#ctyn"
	xtset id year
	eststo clear
	eststo : qui reghdfe D.lv_jst i.`treated'#c.D.ltariff_jst i.`treated'#c.L.lv_jst i.`treated'#c.L.ltariff_jst `controls' if temp1==1, absorb(`fe') vce(`cluster')

	loc treated chn
	loc tstat 1.96
	g temp_beta_ecm=.
	g temp_ub_ecm=.
	g temp_lb_ecm=.

	nlcom (a1: _b[i1.`treated'#c.D.ltariff_jst]) (a2: _b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst] + _b[i1.`treated'#c.D.ltariff_jst]) (a3: (2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a4: (1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a5: (1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a6: (1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a7: (1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a8: (1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a9: (1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a10: (1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a11:  (1+_b[i1.`treated'#c.L.lv_jst])^9*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a12:  (1+_b[i1.`treated'#c.L.lv_jst])^10*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^9*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a13: (1+_b[i1.`treated'#c.L.lv_jst])^11*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^10*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^9*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a14: (1+_b[i1.`treated'#c.L.lv_jst])^12*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^10*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^9*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (a15: (1+_b[i1.`treated'#c.L.lv_jst])^13*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^12*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^10*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^9*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^8*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^7*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^6*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^5*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^4*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^3*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(1+_b[i1.`treated'#c.L.lv_jst])^2*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst])+(2+_b[i1.`treated'#c.L.lv_jst])*(_b[i1.`treated'#c.L.lv_jst]*_b[i1.`treated'#c.D.ltariff_jst]+_b[i1.`treated'#c.L.ltariff_jst]) + _b[i1.`treated'#c.D.ltariff_jst]) (aN: (-_b[i1.`treated'#c.L.ltariff_jst]/(_b[i1.`treated'#c.L.lv_jst]))), post

	forv i=1/15{
		replace temp_beta_ecm = -_b[a`i'] if _n==`i'
		replace temp_ub_ecm = -_b[a`i'] + `tstat' * _se[a`i'] if _n==`i'
		replace temp_lb_ecm = -_b[a`i'] - `tstat' * _se[a`i'] if _n==`i'
	}
	replace temp_beta_ecm = -_b[aN] if _n>15 & _n<=26
	replace temp_ub_ecm = -_b[aN] + `tstat'* _se[aN] if _n>15 & _n<26
	replace temp_lb_ecm = -_b[aN] - `tstat'* _se[aN] if _n>15 & _n<26

	// Figure 2B: Trade Elasticities
	gen temp_yr = _n
	replace temp_yr=. if temp_beta_ecm==.
	
	export delimited temp_beta_ecm temp_yr temp_ub_ecm temp_beta2 temp_ll2 temp_lb_ecm temp_ul2 if temp_yr!=. using "${dir_int}ecm-lp.csv", replace
	
	// Need the long run elasticity for the calibration
	preserve
		drop if temp_yr!=26
		keep temp_beta_ecm		
		save "${dir_int}inputs_calibration_lrelast.dta", replace
	restore
}

////////////////////////////////////////////////////////////////////////////////
// Figure 3
if `f3' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009
	
	loc FE="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	loc x lgap_med
	loc treated chn
	loc controls=""
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	gen temp_b =.
	gen temp_ub =.
	gen temp_lb =.
	eststo clear
	xtset id year
	eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE' vce(`cluster')
	forval t = 1974/2007 {
		qui replace temp_b = _b[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_ub = _b[`t'.year#1.`treated'#c.`x'] + 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
		qui replace temp_lb = _b[`t'.year#1.`treated'#c.`x'] - 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
	}
	sort year
	egen temptag = tag(year)
	
	export delimited temp_b year temp_ub temp_lb if temptag==1 using "${dir_int}gap-elast.csv", replace
	
	// Need these coefficients for the employment analysis later and the calibration.
	preserve
		keep if temptag==1
		keep year temp_b temp_ub temp_lb 
		save "${dir_int}inputs_calibration_gapelast.dta", replace
	restore
	
	drop if temptag==0
	keep year temp_b* temp_?b*
	sort year
	compress
	save "${dir_int}results_annual_gap_baseline.dta", replace
}

////////////////////////////////////////////////////////////////////////////////
// Figure 4 
if `f4' == 1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	gen temp_applied = duties_jst/v_jst
	bysort sitc: egen temp_applied_1979 = mean(temp_applied/(chn==1 & year<1980))
	bysort sitc: egen temp_applied_1981 = mean(temp_applied/(chn==1 & year==1981))
	gen temp_gap = temp_applied_1979-temp_applied_1981
	replace temp_gap=0 if temp_gap<0

	bysort sitc: egen temp_applied1_1979 = mean(tar_unwgt/(chn==1 & year<1980))
	bysort sitc: egen temp_applied1_1981 = mean(tar_unwgt/(chn==1 & year==1981))
	gen temp_gap1 = temp_applied1_1979-temp_applied1_1981
	replace temp_gap1=0 if temp_gap1<0


	loc year 1979
	loc schedule gap_med
	loc applied temp_gap1
	loc country "china"
	gen temp =  year==`year' & cty=="`country'" & phase==0
	
	export delimited `applied' `schedule' if temp==1 using "${dir_int}gap-applied.csv", replace	
}

////////////////////////////////////////////////////////////////////////////////
// Section 5.3 regression
if `s53'==1{
	use "${dir_int}dataset_sitc.dta", clear
	drop if year>=2009

	loc FE="absorb(ctyn#year sitcn#ctyn sitcn#year)"
	loc x lgap_med
	loc treated chn
	loc cluster="cluster sitcn#ctyn"
	gen temp= 1
	eststo clear
	loc controls="ltariff_jst"
	reghdfe lv_jst i0.postWTO#i1.`treated'#c.`x' `controls' if year>1991 & year<2008 & temp==1, `FE' vce(`cluster')
	eststo reg_results
	esttab reg_results using "${dir_int}ps_results.csv", replace
}

////////////////////////////////////////////////////////////////////////////////
// Results needed for the employment analysis
if `cemp' == 1 {
	use "${dir_int}dataset_sitc.dta", clear
	
	loc FE1="absorb(sitcn#year sitcn#ctyn ctyn#year)"
	loc FE2="absorb(ctyn#year sitcn#ctyn)"
	loc FE3="absorb(sitcn#year sitcn#ctyn)"
	loc FE4="absorb(sitcn#ctyn year)"
	loc x lgap_med
	loc treated chn
	loc controls=""
	loc cluster="cluster sitcn#ctyn"
	gen temp= phase==0 & fta==0 & (`treated'==1 | ntr==1)
	eststo clear
	xtset id year
	forval v = 1/4 {
		gen temp`v'_b =.
		gen temp`v'_ub =.
		gen temp`v'_lb =.

		eststo : qui reghdfe lv_jst ibn.year#i1.`treated'#c.`x' `controls' if temp==1, `FE`v'' vce(`cluster')
		forval t = 1974/2008 {
			qui replace temp`v'_b = _b[`t'.year#1.`treated'#c.`x'] if year==`t'
			qui replace temp`v'_ub = _b[`t'.year#1.`treated'#c.`x'] + 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
			qui replace temp`v'_lb = _b[`t'.year#1.`treated'#c.`x'] - 1.96*_se[`t'.year#1.`treated'#c.`x'] if year==`t'
		}
	}
	sort year
	egen temptag = tag(year)
	gen temptradecoeff_base_t = temp1_b
	gen temptradecoeffub_base_t = temp1_ub
	gen temptradecoefflb_base_t = temp1_lb
	gen temptradecoeff_jtNjgFE_t = temp2_b
	gen temptradecoeffub_jtNjgFE_t = temp2_ub
	gen temptradecoefflb_jtNjgFE_t = temp2_lb
	gen temptradecoeff_gtNjgFE_t = temp3_b
	gen temptradecoeffub_gtNjgFE_t = temp3_ub
	gen temptradecoefflb_gtNjgFE_t = temp3_lb
	gen temptradecoeff_jgNtFE_t = temp4_b
	gen temptradecoeffub_jgNtFE_t = temp4_ub
	gen temptradecoefflb_jgNtFE_t = temp4_lb
	
	keep year temptradecoeff* 
	rename temp* *
	duplicates drop
	save "${dir_int}tradecoeffs.dta", replace		
}

timer off 1
timer list
